package com.axzhengxin.platform.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONAware;
import com.alibaba.fastjson.JSONObject;
import com.axzhengxin.platform.common.props.DbExecProps;
import com.axzhengxin.platform.common.service.IDataService;
import com.axzhengxin.platform.common.service.IDbExecService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * DbExecServiceImpl
 *
 * @author jiangnan
 * @date 2021/2/23
 */
@Service
public class DbExecServiceImpl implements IDbExecService {

    private static final String BODY_KEY = "body";

    private static final String RESULT_KEY = "result";

    private DbExecProps dbExecProps;

    private IDataService dataService;

    @Autowired
    public DbExecServiceImpl(DbExecProps dbExecProps, IDataService dataService) {
        this.dbExecProps = dbExecProps;
        this.dataService = dataService;
    }

    @Override
    public JSONArray invoke(String sqlId, Object... args) {
        String url = dbExecProps.getUrl();
        String interfaceNo = dbExecProps.getInterfaceNo();
        return Optional.ofNullable(initRequestBody(sqlId, args))
                .map(JSONAware::toJSONString)
                .map(requestJson -> dataService.invoke(url, requestJson, interfaceNo, Boolean.TRUE))
                .map(JSON::parseObject)
                .filter(response -> response.containsKey(BODY_KEY))
                .map(response -> response.getJSONObject(BODY_KEY))
                .filter(responseBody -> responseBody.containsKey(RESULT_KEY))
                .map(responseBody -> responseBody.getJSONArray(RESULT_KEY))
                .orElse(new JSONArray());
    }

    @Override
    public Optional<JSONObject> getJbxxByEntName(String entName) {
        return Optional.ofNullable(invoke("getJbxxByEntname", entName))
                // 判断至少有一个结果
                .filter(arr -> arr.size() > 0)
                // 取第一个结果
                .map(arr -> arr.getJSONObject(0));
    }

    @Override
    public Optional<String> getLerpNameByEntName(String entName) {
        JSONArray arr = invoke("selectFaName", entName);
        return Optional.ofNullable(IDbExecService.getField(arr, "LEREP"));
    }

    @NotNull
    @Override
    public JSONArray getGdjczdj(String entId) {
        return invoke("queryByMarprid", entId);
    }

    @NotNull
    @Override
    public String getDictValue(String bName, String bType, String bKey) {
        JSONArray result = invoke("getCodeByBnameBtypeBkey", bName, bType, bKey);
        return IDbExecService.getField(result, "BVALUE");
    }

    @Override
    public String getGsCodeLibraryByBkeyBname(String bName, String bKey) {
        JSONArray result = invoke("getGsCodeLibraryByBkeyBname", bKey, bName);
        return IDbExecService.getField(result, "BVALUE");
    }

    @NotNull
    @Override
    public JSONArray getZyry(String entId) {
        return invoke("getDistinctZyryByMarprid", entId);
    }

    @Override
    public JSONArray getJbxxBg(String entId) {
        return invoke("getJbxxbgByMarprid", entId);
    }

    @Override
    public JSONArray getJyyc(String entId) {
        return invoke("getJyycByMarprid", entId);
    }

    @Override
    public JSONArray getXzcf(String entId) {
        return invoke("getXzcfByMarprid", entId);
    }

    @Override
    public String getHshyCodeByColValueAndName(String itemno, String codeno) {
        JSONArray result = invoke("getHshyCodeByColValueAndName", itemno, codeno);
        return IDbExecService.getField(result, "COL_VALUE_COMMENT");
    }

    private JSONObject initRequestBody(String sqlId, Object... args) {
        JSONObject requestBody = new JSONObject();
        requestBody.put("sqlId", sqlId);
        requestBody.put("namespace", dbExecProps.getNamespace());
        JSONArray params = new JSONArray();
        for (int index = 1; index <= args.length; index++) {
            JSONObject param = new JSONObject();
            param.put("index", index);
            param.put("value", args[index - 1]);
            params.add(param);
        }
        requestBody.put("params", params);
        return requestBody;
    }
}
